/**
* 发送get请求
* @param {String} url 请求地址
* @param {String} params 请求参数
* @param {function} headersFn 自定义请求头
* @return Promise 里面存放了成功和失败的数据
*/

// post
function post(url, params, headersFn) {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest  //创建ajax 
        xhr.onreadystatechange = function () { //监听状态
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    let res = JSON.parse(xhr.responseText)
                    resolve(res)
                } else {
                    reject(xhr.status)
                }
            }
        }
        xhr.open('post', url)
        xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded");
        if (headersFn) headersFn(xhr)
        xhr.send(params)
    })
}
// get
function get(url, params, headersFn) {
    return new Promise((resolve, reject) => {
        const xhr = new XMLHttpRequest  //创建ajax 
        xhr.onreadystatechange = function () { //监听状态
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    let res = JSON.parse(xhr.responseText)
                    resolve(res)
                } else {
                    reject(xhr.status)
                }
            }
        }
        xhr.open('GET', `${url}?${params}`)
        if (headersFn) headersFn(xhr)
        xhr.send(null)
    })
}




